<?php

// +----------------------------------------------------------------------
// | ADM Software [ 我宁可 为做过的事而后悔，也不想 因为没尝试而后悔。 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 扬州安蝶梦电子商务有限公司 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 曹梦龙 <138888611@qq.com> <http://www.andiemeng.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Org\Util\Sync;
use Think\Model;

class MorningMarketController extends AdminController {

    //早市商品列表
    public function index() {
        
        $name = I('name');
        $map['pname'] = array('like', '%' . (string) $name . '%');
        $list = $this->lists('vmorningmarket', $map);
        
        int_to_string($list);
        $this->assign('_list', $list);
        
        $this->meta_title = '商品数据列表';
        $this->display();
    }
    
    /**
     * 新增品类
     * @author wangyuanlin
     * 2015-05-17
     */
    public function add($specid = "", $begin = "", $end = "") {
        if (IS_POST) {
            $data = array('specid' => $specid, 'begin' => $begin, 'end' => $end,'createdate' => NOW_TIME, 'lastupdate' => NOW_TIME);
            $id = M('morningmarket')->add($data);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('index'));
            }
        } else {
            $Model = new Model();
            $sql = "select 
                a.id,a.pname,a.name
                from v_product a 
                where a.status = 1
                and not exists (select 1 from gr_morningmarket b where a.id = b.specid and b.status = 1 )
                order by pname,id desc";
            $prolist = $Model -> query($sql);
            $this->_prolist = $prolist;
            
            $this->display();
        }
    }
    
    /**
     * 上推
     * @author wangyuanlin
     * 2015-06-23
     */
    public function push(){
       $reuslt = D("MorningMarketLeanCloud")->push();
       $this->success('水果早市数据上推成功！', U('index'),true);
    }
    
    /**
     * 删除品类
     * @author wangyuanlin
     * 2015-06-24
     */
    public function del($id = 0) {
        if ($id) {
            $market = M("morningmarket");
            
            $data = array('status'=>-1,'syncStatus'=>3);
            $result = $market-> where(array('id'=>$id))->setField($data);
            
            if($result){
                $this->success('删除成功！', U('index'));
            }else{
                $this->error("删除失败！");
            }
        }
    }
}
